package com.newland.esp.service.goods.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newland.esp.common.util.DataResult;
import com.newland.esp.entity.goods.GoodsInfoEntity;
import com.newland.esp.entity.goods.SpAttributeEntity;
import com.newland.esp.entity.goods.SpCategoryEntity;
import com.newland.esp.mapper.goods.GoodsInfoMapper;
import com.newland.esp.mapper.goods.SpAttributeMapper;
import com.newland.esp.service.goods.GoodsInfoService;
import com.newland.esp.vo.response.goods.GoodsInfoSearchVO;
import com.newland.esp.vo.response.user.TableDataVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @param
 * @author lw
 * @descripe
 * @date 2021/10/27 22:01
 * @Version 1.0
 **/
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfoEntity> implements GoodsInfoService {
    @Resource
    GoodsInfoMapper goodsInfoMapper;
    @Resource
    SpAttributeMapper spAttributeMapper;

    @Override
    public TableDataVO<GoodsInfoEntity> findGoodsByPage(Integer page, Integer limit, GoodsInfoSearchVO goodsInfoSearchVO) {
        IPage<GoodsInfoEntity> goodsInfoEntityPage = new Page<>(page,limit);
        QueryWrapper<GoodsInfoEntity> wrapper = new QueryWrapper<>();
        String goodsName = null;
        String catId = null;
        if(goodsInfoSearchVO != null){
            goodsName = goodsInfoSearchVO.getGoodsName();
            catId = goodsInfoSearchVO.getCatId();
        }
        if (!StringUtils.isEmpty(goodsName)){
            wrapper.like("goods_name",goodsName);
        }
        if (!StringUtils.isEmpty(catId)){
            wrapper.eq("cat_id",catId);
        }
        IPage<GoodsInfoEntity> result = goodsInfoMapper.selectPage(goodsInfoEntityPage, wrapper);
        TableDataVO<GoodsInfoEntity> dataResult = new TableDataVO<>();
        dataResult.setData(result.getRecords());
        dataResult.setCount(result.getTotal());
        dataResult.setCode(200);
        dataResult.setMsg("操作成功");
        return dataResult;
    }

    @Override
    public DataResult categoryChildrenList() {
        List<SpCategoryEntity> result = goodsInfoMapper.getCategoryChildrenList();
        return DataResult.success(result);
    }

    @Override
    public DataResult attributeList(Short catId, Object attrSel) {
        QueryWrapper<SpAttributeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_id",catId);
        wrapper.eq("attr_sel",attrSel);
        List<SpAttributeEntity> result = spAttributeMapper.selectList(wrapper);
        return DataResult.success(result);
    }
}
